package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.service.AdminKeywordService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class AdminKeywordServiceImpl implements AdminKeywordService {

    //list
    @Override
    public List<MarketKeyword> list(String page, String limit, String sort, String order, String keyword, String url) {

        //获得session
        SqlSession session = MybatisUtil.getSession();
        MarketKeywordMapper keywordMapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria or = marketKeywordExample.or();

        //判断是否有查找
        if (!StringUtils.isEmpty(keyword)){
            or.andKeywordLike("%" + keyword + "%");
        }
        if (!StringUtils.isEmpty(url)){
            or.andUrlLike("%" + url + "%");
        }

        //过滤逻辑删除
        or.andLogicalDeleted(true);

        //排序
        marketKeywordExample.orderBy(sort+" "+order);

        //分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(marketKeywordExample);
        session.commit();
        session.close();
        return marketKeywords;
    }

    //create
    @Override
    public MarketKeyword create(String url, String keyword, String isHot, String isDefault) {

        //获得session
        SqlSession session = MybatisUtil.getSession();
        MarketKeywordMapper keywordMapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeyword marketKeyword = new MarketKeyword();

        //添加数据
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        marketKeyword.setIsDefault(Boolean.valueOf(isDefault));
        marketKeyword.setIsHot(Boolean.valueOf(isHot));
        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setDeleted(false);

        //插入数据
        keywordMapper.insertSelective(marketKeyword);
        //keywordMapper.insert(marketKeyword);
        session.commit();
        session.close();

        return marketKeyword;

    }

    @Override
    public MarketKeyword update(String isDefault, String isHot,
                                String addTime, String keyword,
                                String url, String id, String updateTime, String sortOrder) {
        //获得session
        SqlSession session = MybatisUtil.getSession();
        MarketKeywordMapper keywordMapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeyword marketKeyword = new MarketKeyword();

        //添加数据
        marketKeyword.setSortOrder(Integer.valueOf(sortOrder));
        marketKeyword.setId(Integer.valueOf(id));
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        marketKeyword.setIsDefault(Boolean.valueOf(isDefault));
        marketKeyword.setIsHot(Boolean.valueOf(isHot));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(addTime, formatter);
        marketKeyword.setAddTime(dateTime);
        marketKeyword.setUpdateTime(LocalDateTime.now());

        //更新信息
        keywordMapper.updateByPrimaryKeySelective(marketKeyword);
        session.commit();
        session.close();

        return marketKeyword;
    }

    @Override
    public void delete(String id) {

        SqlSession session = MybatisUtil.getSession();
        MarketKeywordMapper keywordMapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeyword marketKeyword = new MarketKeyword();

        //添加逻辑删除数据
        marketKeyword.setId(Integer.valueOf(id));
        marketKeyword.setDeleted(true);

        keywordMapper.updateByPrimaryKeySelective(marketKeyword);

        session.commit();
        session.close();
    }
}
